package common.util.enums;




import java.io.Serializable;
import java.lang.reflect.*;
import java.util.*;

// Referenced classes of package com.kingdee.util.enums:
//            EnumResourceProviderManager

public abstract class Enum
    implements Comparable, Serializable
{
    private static class Entry
    {

        final Map map;
        final List list;

        private Entry()
        {
            map = new HashMap(50);
            list = new ArrayList(25);
        }

    }


    protected Enum(String name)
    {
        if(name == null || name.length() == 0)
            throw new IllegalArgumentException("The Enum name must not be empty");
        iName = name;
        Entry entry = (Entry)cEnumClasses.get(getClass().getName());
        if(entry == null)
        {
            entry = new Entry();
            cEnumClasses.put(getClass().getName(), entry);
        }
        if(entry.map.containsKey(name))
        {
            throw new IllegalArgumentException("The Enum name must be unique, '" + name + "' has already been added");
        } else
        {
            entry.map.put(name, this);
            entry.list.add(this);
            return;
        }
    }

    protected Object readResolve()
    {
        return getEnum(getClass(), getName());
    }

    protected static Enum getEnum(Class enumClass, String name)
    {
        if(enumClass == null)
            throw new IllegalArgumentException("The Enum Class must not be null");
        Entry entry = (Entry)cEnumClasses.get(enumClass.getName());
        if(entry == null)
            return null;
        else
            return (Enum)entry.map.get(name);
    }

    protected static Map getEnumMap(Class enumClass)
    {
        if(enumClass == null)
            throw new IllegalArgumentException("The Enum Class must not be null");
        if(!(common.util.enums.Enum.class).isAssignableFrom(enumClass))
            throw new IllegalArgumentException("The Class must be a subclass of Enum");
        Entry entry = (Entry)cEnumClasses.get(enumClass.getName());
        if(entry == null)
            return EMPTY_MAP;
        else
            return Collections.unmodifiableMap(entry.map);
    }

    protected static List getEnumList(Class enumClass)
    {
        if(enumClass == null)
            throw new IllegalArgumentException("The Enum Class must not be null");
        if(!(common.util.enums.Enum.class).isAssignableFrom(enumClass))
            throw new IllegalArgumentException("The Class must be a subclass of Enum");
        Entry entry = (Entry)cEnumClasses.get(enumClass.getName());
        if(entry == null)
        {
            getStaticFields(enumClass);
            entry = (Entry)cEnumClasses.get(enumClass.getName());
            if(entry == null)
                return Collections.EMPTY_LIST;
        }
        return Collections.unmodifiableList(entry.list);
    }

    protected static List getEnumList(Class enumClass, Locale locale)
    {
        if(enumClass == null)
            throw new IllegalArgumentException("The Enum Class must not be null");
        if(!(common.util.enums.Enum.class).isAssignableFrom(enumClass))
            throw new IllegalArgumentException("The Class must be a subclass of Enum");
        Entry entry = (Entry)cEnumClasses.get(enumClass.getName());
        if(entry == null)
            return Collections.EMPTY_LIST;
        else
            return Collections.unmodifiableList(entry.list);
    }

    protected static String[] getEnumNames(Class enumClass)
    {
        List enums = getEnumList(enumClass);
        String names[] = new String[enums.size()];
        for(int i = 0; i < enums.size(); i++)
        {
            Enum _enum = (Enum)enums.get(i);
            names[i] = _enum.getName();
        }

        return names;
    }

    protected static Iterator iterator(Class enumClass)
    {
        return getEnumList(enumClass).iterator();
    }

    public final String getName()
    {
        return iName;
    }

    public final boolean equals(Object other)
    {
        if(other == this)
            return true;
        if(other == null)
            return false;
        if(other.getClass() == getClass())
            return iName.equals(((Enum)other).iName);
        if(other.getClass().getName().equals(getClass().getName()))
        {
            try
            {
                return iName.equals(((Enum)other).iName);
            }
            catch(ClassCastException ex) { }
            try
            {
                Method mth = other.getClass().getMethod("getName", null);
                String name = (String)mth.invoke(other, null);
                return iName.equals(name);
            }
            catch(NoSuchMethodException ex2) { }
            catch(IllegalAccessException ex2) { }
            catch(InvocationTargetException ex2) { }
            return false;
        } else
        {
            return false;
        }
    }

    public final int hashCode()
    {
        return 7 + iName.hashCode();
    }

    public int compareTo(Object other)
    {
        return iName.compareTo(((Enum)other).iName);
    }

    public String toString()
    {
        return getAlias(null);
    }

    public String getAlias()
    {
        return getAlias(null);
    }

    public String getAlias(Locale locale)
    {
        Locale thisLocale = locale;
        if(locale == null)
            try
            {
                Class clazz = Class.forName("com.kingdee.bos.ContextUtils");
                Method m = clazz.getMethod("getOriginalLocaleFromEnv", null);
                thisLocale = (Locale)m.invoke(null, null);
               // if(thisLocale == null)
                    //thisLocale = LocaleUtils.getDefaultLocale();
            }
            catch(Throwable t)
            {
               // thisLocale = LocaleUtils.getDefaultLocale();
            }
//        if(EnumResourceProviderManager.fromDB())
//        {
//            String s = EnumResourceProviderManager.getString(getClass().getName(), thisLocale, getName());
//            if(!StringUtils.isEmpty(s))
//                return s;
//        }
        ResourceBundle res = null;
        try
        {
//            if(EnumResourceProviderManager.fromFile())
//            {
//                res = BOSResourceBundle.getBundle(getClass().getName(), thisLocale);
//                return res.getString(getName());
//            }
        }
        catch(MissingResourceException mre)
        {
            return getName();
        }
        return getName();
    }

    protected static String[] getEnumAliases(Class enumClass, Locale locale)
    {
        List enums = getEnumList(enumClass);
        String aliases[] = new String[enums.size()];
        for(int i = 0; i < enums.size(); i++)
        {
            Enum _enum = (Enum)enums.get(i);
            aliases[i] = _enum.getAlias(locale);
        }

        return aliases;
    }

    public static void getStaticFields(Class enumCalss)
    {
        Field fields[] = enumCalss.getDeclaredFields();
        for(int i = 0; i < fields.length; i++)
        {
            if((fields[i].getModifiers() & 8) == 0 || (fields[i].getModifiers() & 1) == 0)
                continue;
            try
            {
                fields[i].get(null);
                break;
            }
            catch(IllegalArgumentException e) { }
            catch(IllegalAccessException e) { }
        }

    }

    private static final long serialVersionUID = -2144611916495959351L;
    private static final Map EMPTY_MAP = Collections.unmodifiableMap(new HashMap());
    private static final Map cEnumClasses = new HashMap();
    private final String iName;

}


/*
	DECOMPILATION REPORT

	Decompiled from: E:\work\SHDC\lib\common\bos\common.jar
	Total time: 31 ms
	Jad reported messages/errors:
	Exit status: 0
	Caught exceptions:
*/